<template>
  <div class="wh100">
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item
              label="用户名"
              prop="nickname"
              :rules="[
              {
                required: true,
                message: '请输入用户名',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.nickname" :disabled="modeType !== 1"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
              label="账号"
              prop="username"
              :rules="[
              {
                required: true,
                message: '请输入账号',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.username"               :disabled="modeType !== 1"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
              label="工号"
              prop="postNo"
              :rules="[
              {
                required: modeType === 1,
                message: '请输入工号',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.postNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
              label="手机号"
              prop="phone"
              :rules="[
              {
                required: modeType === 1,
                message: '请输入手机号',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.phone"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="modeType === 1">
          <el-form-item
              label="密码"
              prop="password"
              :rules="[
              {
                required: modeType === 1,
                message: '请输入密码',
                trigger: 'blur',
              },
            ]"
          >
            <el-input v-model="form.password" type="password"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item
              label="所属部门"
              prop="deptIds"
              :rules="[
              {
                required: modeType === 1,
                message: '请选择所属部门',
                trigger: 'blur',
              },
            ]"
          >
            <el-select
                multiple
                collapse-tags
                v-model="form.deptIds"
                placeholder="请选择所属部门"
            >
              <el-option
                  v-for="item in deptList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item
              label="功能角色"
              prop="roleIds"
              :rules="[
              {
                required: modeType === 1,
                message: '请选择功能角色',
                trigger: 'blur',
              },
            ]"
          >
            <el-select
                multiple
                collapse-tags
                v-model="form.roleIds"
                placeholder="请选择"
            >
              <el-option
                  v-for="item in roleList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="modeType === 1">
          <el-form-item label="状态" :required="modeType === 1">
            <el-row :gutter="24">
              <el-col :span="12">
                <el-switch v-model="form.isDisable"></el-switch>
                <span class="left10" v-if="form.isDisable >= 1"> 禁用</span>
                <span class="left10" v-else> 启用</span>
              </el-col>
            </el-row>
          </el-form-item>
        </el-col>

        <el-col :span="24" style="text-align: right">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button type="primary" @click="onSubmit('form')">保存</el-button>
              </div>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import {getDeptAll} from "@/api/dept";
import {getRoleList} from "@/api/role";
import {addAdmin, editAdmin} from "@/api/admin";

export default {
  props: {
    modeType: {
      type: Number,
      default: 0,
    },
    adminInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {
        phone:"",
        postNo:"",
        nickName: "1",
        username: "",
        password: "",
        isDisable: "",
        roleIds: [],
        deptIds: [],
      },

      deptList: [],
      roleList: [],
      loading: false,
    };
  },
  mounted() {
    this.getDeptAll();
    this.getRoleList();

    if (this.modeType != 1) {
      this.form = JSON.parse(JSON.stringify(this.adminInfo));
      console.log("this.form",this.form)
      // this.form.isDisable = this.form.isDisable == 0 ? true : false;
    }
  },
  methods: {
    //查询所有部门
    async getDeptAll() {
      let res = await getDeptAll();
      if (res.code == 200) {
        this.deptList = res.data;
      }
    },

    async getRoleList() {
      let res = await getRoleList({
        pageNo: 1,
        pageSize: 59,
      });
      if (res.code == 200) {
        this.roleList = res.data.lists;
      }
    },

    async editAdmin() {
      let res = await editAdmin({
        id: this.adminInfo.id,
        roleIds: this.form.roleIds,
        isDisable: this.form.isDisable ? 1 : 0,
        password: this.form.password,
        nickname: this.form.nickname,
        username: this.form.username,
        deptIds: this.form.deptIds,
        phone: this.form.phone,
        postNo:this.form.postNo
      });
      if (res.code == 200) {
        this.loading = false;
        this.$message({
          message: "编辑成功",
          type: "success",
        });
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },

    async addAdmin() {
      let res = await addAdmin({
        roleIds: this.form.roleIds,
        isDisable: this.form.isDisable ? 1 : 0,
        password: this.form.password,
        nickname: this.form.nickname,
        username: this.form.username,
        deptIds: this.form.deptIds,
        phone: this.form.phone,
        postNo:this.form.postNo
      });
      if (res.code == 200) {
        this.loading = false;
        this.$message({
          message: "添加成功",
          type: "success",
        });
        this.$emit("onClose", {
          isClose: true,
        });
      }
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        console.log(valid);
        if (valid) {
          this.loading = true;
          if (this.modeType == 1) {
            this.addAdmin();
          } else if (this.modeType == 3) {
            this.editAdmin();
          }
        } else {
          console.log("error");
        }
      });
    },

    cancelModel() {
      this.$emit("onClose", {isClose: false});
    },
  },
};
</script>
<style lang="scss" scoped>
.left10 {
  display: inline-block;
  margin-left: 10px;
  color: #fff;
}
.btn {
  display: flex;
  gap: 20px;
  justify-content: center;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}

::v-deep .el-select {
  width: 100%;
}
</style>

<style>


</style>
